import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:wechat_lite/providers/auth_provider.dart';
import 'package:wechat_lite/providers/chat_provider.dart';
import 'package:wechat_lite/screens/login_screen.dart';
import 'package:wechat_lite/screens/chat_screen.dart';
import 'package:wechat_lite/services/socket_service.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => AuthProvider()),
        ChangeNotifierProvider(create: (_) => ChatProvider()),
        Provider(create: (_) => SocketService()),
      ],
      child: MaterialApp(
        title: 'WeChat-Lite',
        theme: ThemeData(
          primarySwatch: Colors.green,
          visualDensity: VisualDensity.adaptivePlatformDensity,
        ),
        home: AuthWrapper(),
      ),
    );
  }
}

class AuthWrapper extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Consumer<AuthProvider>(
      builder: (context, authProvider, _) {
        return FutureBuilder(
          future: authProvider.checkAuth(),
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting) {
              return Scaffold(
                body: Center(child: CircularProgressIndicator()),
              );
            }
            
            if (authProvider.isAuthenticated) {
              return ChatScreen();
            } else {
              return LoginScreen();
            }
          },
        );
      },
    );
  }
}
